# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=curl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.71.1
pkgrel=2
pkgdesc="Command line tool and library for transferring data with URLs. (mingw-w64)"
arch=('any')
url="https://curl.haxx.se"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libmetalink"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-ca-certificates"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-nghttp2"
         "${MINGW_PACKAGE_PREFIX}-brotli")
options=('staticlibs')
source=("${url}/download/${_realname}-${pkgver}.tar.bz2"{,.asc}
        "0001-Make-cURL-relocatable.patch"
        "0002-Hack-make-relocation-work-inside-libexec-git-core-an.patch")
sha256sums=('9d52a4d80554f9b0d460ea2be5d7be99897a1a9f681ffafe739169afd6b4f224'
            'SKIP'
            'a99d62c3e0c554c25916cbb1114f94f2243c1cb68f531e80db95788331062722'
            'b3c6fae8adb226078f502ddfe468f5ad239dfeeebbb4ec9ecd14a93a03db4ce3')
validpgpkeys=('914C533DF9B2ADA2204F586D78E11C6B279D5C91'  # Daniel Stenberg
              '27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2'
              '4461EAF0F8E9097F48AF0555F9FEAFF9D34A1BDB')

if test -z "$WITHOUT_PDBS"
then
	pkgname+=("${MINGW_PACKAGE_PREFIX}-${_realname}-pdb")
	makedepends+=("${MINGW_PACKAGE_PREFIX}-cv2pdb")
	options+=('!strip')
else
	options+=('strip')
fi

prepare() {
  cd "${_realname}-${pkgver}"
  # MINGW builds typically mean that prepare is run twice: once for 64-bit and
  # once for 64-bit. These files are therefore added twice by below patches, so
  # let's force-delete them before trying to add them.
  rm -f lib/pathtools.h lib/pathtools.c tests/data/test2070 >/dev/null 2>&1 || true
  patch -p1 -i "${srcdir}/0001-Make-cURL-relocatable.patch"
  patch -p1 -i "${srcdir}/0002-Hack-make-relocation-work-inside-libexec-git-core-an.patch"
  autoreconf -vfi
}

build() {
  local -a extra_config
  extra_config+=( --disable-debug )
  mkdir -p "${srcdir}/build-${CARCH}"
  extra_config+=("--with-ssl")
  extra_config+=("--with-ca-bundle=${MINGW_PREFIX}/ssl/certs/ca-bundle.crt")
  extra_config+=('--with-nghttp2=${MINGW_PREFIX}/')
  extra_config+=("--with-winssl")
  cd "${srcdir}/build-${CARCH}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-random \
    --without-librtmp \
    --enable-static \
    --enable-shared \
    --enable-sspi \
    --with-libmetalink=${MINGW_PREFIX} \
    --with-libssh2 \
    "${extra_config[@]}"
# there's a bug with zsh completion generation script and Windows.
# curl has to specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make
  case "${pkgname[@]}"
  in *pdb*)
    cv2pdb lib/.libs/libcurl-4.dll
    cv2pdb src/.libs/curl.exe
  esac
}

package_1() {
  cd "${srcdir}/build-${CARCH}"

  test pdb != "$1" || {
    install -d -m 755 "${pkgdir}${MINGW_PREFIX}/bin"
    install -m 755 lib/.libs/libcurl-4.pdb "${pkgdir}${MINGW_PREFIX}/bin"
    install -m 755 src/.libs/curl.pdb "${pkgdir}${MINGW_PREFIX}/bin"
    return
  }

  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/curl-config
}

package_mingw-w64-i686-curl() {
  package_1
}

package_mingw-w64-x86_64-curl() {
  package_1
}

package_mingw-w64-i686-curl-pdb() {
  package_1 pdb
}

package_mingw-w64-x86_64-curl-pdb() {
  package_1 pdb
}
